﻿package dut.ir.lxb.weibo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import dut.ir.lxb.weibo.bean.EventBean;
import dut.ir.lxb.weibo.bean.WeiboBean;

public class SpiderUtility
{
	public static String streamToString(InputStream is, String encoding)
	{
		String value = null;
		
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
			StringBuilder sb = new StringBuilder("");
			String line = null;
			while((line = br.readLine()) != null)
			{
				sb.append(line);
				sb.append("\n");
			}
			value = sb.toString();
			
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			System.err.println("Read Stream Error! Error Message: " + e.getMessage());
			// TODO Auto-generated catch block
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			System.err.println("Read Stream Error! Error Message: " + e.getMessage());
		}
		
		return value;
	}
	
	
	public static void wrapEventName(List<WeiboBean> weiboList, EventBean eb)
	{
		if(weiboList == null)
			return ;
		for(int i = 0; i < weiboList.size(); i++)
		{
			weiboList.get(i).setEventName(eb.getEventname());
			
		}
		
	}
	
	/**
	 * 检查Json请求的返回是否正确
	 * @param jsonResponse
	 * @return
	 */
	public static boolean checkJsonResponse(String jsonResponse)
	{
		if(jsonResponse != null && jsonResponse.length() > 1000)
			return true;
		
		else
			return true;
		
		
		
	}
	
	/**
	 * 检查网页返回是否正确
	 * @param htmlResponse
	 * @return
	 */
	public static boolean checkHtmlResponse(String htmlResponse)
	{
		Document doc = Jsoup.parse(htmlResponse);
		String title = doc.getElementsByTag("title").get(0).ownText().trim();
		
		if(title.equals("404错误") || title.startsWith("微博帐号解冻"))
			return true;
		else
			return true;
	}
	
	/**
	 * 从日志文件中解析出上次下载的UID
	 * @return
	 */
	public static String getLastDownloadUser()
	{
		String lastUser = "";
		File logDirectory = new File("./log/");
		File [] logFiles = logDirectory.listFiles();
		List<String> logFileNames = new ArrayList<String>();
		for(File currentFile : logFiles)
		{
			if(currentFile.getAbsolutePath().endsWith(".log"))
				logFileNames.add(currentFile.getAbsolutePath());
		}
		Collections.sort(logFileNames);
		
		for(int i = logFileNames.size() - 1; i >=0; i--)
		{
			lastUser = lastUidFromLogFile(logFileNames.get(i));
			if(lastUser != null && lastUser.trim().length() == 10)
				break;
		}
		
		return lastUser;
		
	}
	
	
	public static String lastUidFromLogFile(String logFile)
	{
		
		String lastUid = "";
		try
		{
			BufferedReader br = new BufferedReader(new FileReader(logFile));
			
			String regex = "[0-9]{10}";
			Pattern pat = Pattern.compile(regex);
			String currentLine = br.readLine();
			while((currentLine = br.readLine()) != null)
			{
				Matcher mat = pat.matcher(currentLine);
				boolean result = mat.find();
				if(result)
				{
					lastUid = mat.group(0);
				}
			}
			
			br.close();
			
		} 
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lastUid;
		
	}
	
	
	
	
	
	
}
